﻿using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows;

namespace IOA.MES.WebTool.Common
{
    /// <summary>
    /// 本地环境
    /// </summary>
    public class BaseEnv
    {
        /// <summary>
        /// 接口根地址
        /// </summary>
        public static string ApiRoot { get; set; }

        /// <summary>
        /// 上传文件接口地址
        /// </summary>
        public static string Ftp { get; set; }

        /// <summary>
        /// 上传文件账号
        /// </summary>
        public static string FtpUserName { get; set; }

        /// <summary>
        /// 上传文件密码
        /// </summary>
        public static string FtpPassword { get; set; }

        /// <summary>
        /// 打印机
        /// </summary>
        public static string MarkPrinter { get; set; }

        /// <summary>
        /// 打印机
        /// </summary>
        public static string LabelPrinter { get; set; }

        /// <summary>
        /// 图片下载位置
        /// </summary>
        public static string ImageFolder { get; set; }

        /// <summary>
        /// 请求Api
        /// </summary>
        public static ResultJson<T> InvokeApi<T>(string path, string token, object data, string apiRoot = "") where T : class
        {
            try
            {
                var header = new Dictionary<string, string>
                {
                    { "Token", token },
                };
                apiRoot = string.IsNullOrWhiteSpace(apiRoot) ? ApiRoot : apiRoot;
                var responseStr = new HttpHelper { DictHeaders = header, UserAgent = "IOA.MES.WebTool" }.PostJson($"{apiRoot}{path}", data);
                var response = JsonConvert.DeserializeObject<ResultJson<T>>(responseStr);
                return response;
            }
            catch (WebException ex)
            {
                return new ResultJson<T> { ErrCode = (int)ex.Status, ErrMsg = $"网络异常：{ex.Message}" };
            }
            catch (Exception ex)
            {
                return new ResultJson<T> { ErrCode = 500, ErrMsg = $"网络异常：{ex.Message}" };
            }
        }

        /// <summary>
        /// 本地根目录
        /// </summary>
        public static string LocalDirectory
        {
            get { return $"{Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)}"; }
        }

        //注册表地址
        private static string regPath = @"HKEY_LOCAL_MACHINE\Software\Aisun\LocalTools";
        public static void CheckConfig()
        {
            try
            {
                var configValue = Convert.ToString(Registry.GetValue(regPath, "Config", ""));
                if (!string.IsNullOrWhiteSpace(configValue))
                {
                    return;
                }

                var dictStorage = new Dictionary<string, string>
                    {
                        { "ApiRoot", "http://192.168.1.233:1001"},
                        { "Ftp", "ftp://10.10.94.3/homes" },
                        { "FtpUserName", "adminnas" },
                        { "FtpPassword", "Dangerous@1987"}
                    };
                configValue = JsonConvert.SerializeObject(dictStorage);
                Registry.SetValue(regPath, "Config", configValue);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"注册表修改失败，请联系IT：{ex}");
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public static void SetStorage(string key, string value)
        {
            var configValue = Convert.ToString(Registry.GetValue(regPath, "Config", ""));
            var dictStorage = JsonConvert.DeserializeObject<Dictionary<string, string>>(configValue);
            if (dictStorage.ContainsKey(key))
            {
                dictStorage[key] = value;
            }
            else
            {
                dictStorage.Add(key, value);
            }

            Registry.SetValue(regPath, "Config", JsonConvert.SerializeObject(dictStorage));
        }

        /// <summary>
        /// 读取存储
        /// </summary>
        public static string GetStorage(string key)
        {
            var configValue = Convert.ToString(Registry.GetValue(regPath, "Config", ""));
            var dictStorage = JsonConvert.DeserializeObject<Dictionary<string, string>>(configValue);
            return (dictStorage == null || !dictStorage.ContainsKey(key)) ? null : dictStorage[key];
        }
    }
}
